//
//  ASJObject.m
//  ASJ001
//
//  Created by zephyrhan on 11/13/11.
//  Copyright (c) 2011 Ebrothers tech. All rights reserved.
//

#import "ASJObject.h"

@implementation ASJObject

@synthesize movingSpeed, attackRate, ATP, HP, attackRange, staySprite;
@synthesize currentHP, currentMovingSpeed, currentAttackRate, currentSkill;
@synthesize worldLayer, worldStage;
@synthesize position;


- (void)dealloc {
    [skills release];
    [staySprite release];
    [currentSkill release];
    
    [super dealloc];
}


- (void)setSkill:(ASJSkill*)skill forName:(NSString*)name {
    if (skills == nil) {
        skills = [[NSMutableDictionary alloc] initWithCapacity:7];
    }

    [skills setObject:skill forKey:name];
}


- (ASJSkill*)skillByName:(NSString*)name {
    return [skills objectForKey:name];
}


- (ASJRunSkillResult)runSkillByName:(NSString*)name {
    ASJSkill* skill = [self skillByName:name];
    
    //CCLog(@"skill:%@ stopable:%d coolTime:%f", skill.skillName, skill.currentStopable, skill.currentCoolTime);
    
    if (skill == nil) {
        return ASJRUNSKILLRET_NOTFOUND;
    } else if ((currentSkill == skill) && (currentSkill.skillPriority == currentSkill.currentPriority)) {
        // skill is running
        return ASJRUNSKILLRET_OK;
    } else if ((currentSkill != nil) && (skill.skillPriority < currentSkill.currentPriority)) {
        return ASJRUNSKILLRET_NOTSTOPABLE;
    } else if (skill.currentCoolTime > 0) {
        return ASJRUNSKILLRET_COOLING;
    }

    CGPoint pos = ccp(-100, -100);
    if (currentSkill.skillSprite != nil) {
        pos = currentSkill.skillSprite.position;
    }

    [currentSkill stopSkill];
    currentSkill = skill;
    [skill fireSkillAtPosition:pos];
    return ASJRUNSKILLRET_OK;
}


- (ASJRunSkillResult)runSkillByName:(NSString*)name frameCount:(NSInteger)count {
    if ([self skillByName:name] == nil) {
        ASJSkill* skill = [[ASJSkill alloc] initWithSkillName:name frameCount:count inStage:worldStage];
        if (skill != nil) {
            [self setSkill:skill forName:name];
            [skill release];
        } else {
            return ASJRUNSKILLRET_NOTFOUND;
        }
    }
    
    return [self runSkillByName:name];
}


- (CGPoint)position {
    return currentSkill.skillSprite.position;
}


- (void)setPosition:(CGPoint)pos {
    currentSkill.skillSprite.position = pos;
}

@end
